package com.smartpro.mis.modular.business.controller;

import com.alibaba.fastjson.JSONObject;
import com.smartpro.mis.core.base.controller.BaseController;
import com.smartpro.mis.modular.business.neo4jEntity.N4jUser;
import com.smartpro.mis.modular.business.service.IMessageService;
import com.smartpro.mis.modular.business.service.IPositionService;
import com.smartpro.mis.modular.business.service.IUserService;
import com.smartpro.mis.modular.system.model.Message;
import com.smartpro.mis.modular.system.model.Position;
import com.smartpro.mis.modular.system.model.User;
import com.smartpro.mis.rest.modular.auth.controller.dto.ResponseBean;
import com.smartpro.mis.rest.modular.auth.util.JWTToken;
import com.smartpro.mis.rest.modular.auth.util.JwtTokenUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;
import org.springframework.beans.factory.annotation.Autowired;
import com.smartpro.mis.modular.system.model.SettingMsg;
import com.smartpro.mis.modular.business.service.ISettingMsgService;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 设置控制器
 *
 * @author Mengiy
 * @Date 2019-05-07 21:48:53
 */
@RestController
@RequestMapping("/api/service/setting")

public class SettingMsgController extends BaseController {


    @Autowired
    private ISettingMsgService settingService;

    @Autowired
    private IUserService userService;




    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 获取设置列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    @RequiresAuthentication
    public Object list(String condition) {
        return settingService.selectList(null);
    }


    /**
     * 获取全部设置列表
     */
    @ApiOperation(value = "获取全部设置列表",notes = "运营端获取全部设置列表")
    @RequestMapping(value = "/getSettingMsgList",method = RequestMethod.GET)
    @ResponseBody
    @RequiresAuthentication
    public Object getSettingMsgList() {
        return new ResponseBean(200,"query success",settingService.selectList(null));
    }

    /**
     * 多条件查询设置列表
     */
    @ApiOperation(value = "多条件查询设置列表",notes="多条件筛选设置列表" )
    @ApiImplicitParam(name = "params", value = "json格式查询条件", required = true, dataType = "Map")
    @RequestMapping(value = "/mulQuery",method = RequestMethod.POST)
    @ResponseBody
    @RequiresAuthentication
    public Object mulQuery(@RequestBody Map<String,Object> params) {
        return new ResponseBean(200,"operation success",settingService.selectByMap(params));
    }

    /**
     * 添加设置
     */
    @ApiOperation(value ="添加设置",notes="登录用户申请成为设置")
    @ApiImplicitParam(name = "disInfo", value = "设置详情", required = true, dataType = "SettingMsg")
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    @RequiresAuthentication
    public Object add(HttpServletRequest request, @RequestBody SettingMsg setting) {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        String authorization = httpServletRequest.getHeader("Authorization");
//        String authorization = request.getUserPrincipal().getName();
        JwtTokenUtil jwt = new JwtTokenUtil();
        String username = jwt.getUsernameFromToken(authorization);
        System.out.println("###########  username from token: "+ username);
        Map<String,Object> condtion = new HashMap();
        condtion.put("wechat",username);
        User user = this.userService.selectByMap(condtion).get(0);
       
        setting.setCreateTime(sdf.format(new Date()));
        setting.setOperatorID(user.getUserID());
        
        try {
            settingService.insertOrUpdate(setting);
            return new ResponseBean(200, "apply submit success", setting);
        }catch (Exception e){
            return new ResponseBean(400, "apply submit fail", e.getStackTrace());
        }

    }


    /**
     * 删除设置
     */
    @ApiOperation(value ="删除设置")
    @ApiImplicitParam(name = "settingId", value = "设置编号", required = true, dataType = "Integer")
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    @ResponseBody
    @RequiresAuthentication
    public Object delete(@RequestParam Integer settingId) {
        settingService.deleteById(settingId);
        return SUCCESS_TIP;
    }

    /**
     * 修改设置
     */
    @ApiOperation(value ="修改设置")
    @ApiImplicitParam(name = "setting", value = "设置详情", required = true, dataType = "SettingMsg")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    @RequiresAuthentication
    public Object update(HttpServletRequest request,@RequestBody SettingMsg setting) {
        setting.setModifyTime(sdf.format(new Date()));
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        String authorization = httpServletRequest.getHeader("Authorization");
        JwtTokenUtil jwt = new JwtTokenUtil();
        String username = jwt.getUsernameFromToken(authorization);
        Map<String,Object> condtion = new HashMap();
        condtion.put("wechat",username);
        User user = this.userService.selectByMap(condtion).get(0);
        setting.setOperatorID(user.getUserID());
        try {
            settingService.updateById(setting);
            return new ResponseBean(200, "update success", setting);
        }catch (Exception e){
            return new ResponseBean(400, "update fail", e.getStackTrace());
        }
    }
    /**
     * 修改设置
     */
    @ApiOperation(value ="按名称修改设置")
    @ApiImplicitParam(name = "setting", value = "设置详情", required = true, dataType = "SettingMsg")
    @RequestMapping(value = "/updateByName",method = RequestMethod.POST)
    @ResponseBody
    @RequiresAuthentication
    public Object updateByName(HttpServletRequest request,@RequestBody SettingMsg setting) {
        setting.setModifyTime(sdf.format(new Date()));
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        String authorization = httpServletRequest.getHeader("Authorization");
        JwtTokenUtil jwt = new JwtTokenUtil();
        String username = jwt.getUsernameFromToken(authorization);
        Map<String,Object> condtion = new HashMap();
        condtion.put("wechat",username);
        User user = this.userService.selectByMap(condtion).get(0);
        setting.setOperatorID(user.getUserID());
        try {
            settingService.updateByName(setting.getSettingValue(), setting.getSettingName(),setting.getModifyTime(),setting.getOperatorID());
            return new ResponseBean(200, "update success", setting);
        }catch (Exception e){
            return new ResponseBean(400, "update fail", e.getStackTrace());
        }
    }  

    /**
     * 设置详情
     */
    @ApiOperation(value ="设置详情")
    @ApiImplicitParam(name = "settingId", value = "设置编号", required = true, dataType = "Integer")
    @RequestMapping(value = "/detail/{settingId}",method = RequestMethod.GET)
    @ResponseBody
    @RequiresAuthentication
    public Object detail(@PathVariable("settingId") Integer settingId) {
        try {

            SettingMsg setting = settingService.selectById(settingId);

            return new ResponseBean(200, "view success", setting);
        } catch (Exception e) {
            return new ResponseBean(400, "view fail", e.getStackTrace());
        }
    }
}
